package xyz.maxwells.bigdata.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.maxwells.bigdata.domain.RequestResult;
import xyz.maxwells.bigdata.domain.SysUser;
import xyz.maxwells.bigdata.domain.data.MyCollect;
import xyz.maxwells.bigdata.exception.BigdataException;
import xyz.maxwells.bigdata.service.MyCollectService;

import java.util.Date;

/**
 * Created by wuzusheng on 2018/5/13.
 */
@RestController
public class MyCollectCtrl {
    @Autowired
    private MyCollectService service;
    //展示所有收藏
    @RequestMapping("/collect")
    public RequestResult findByName()throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser securityUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new RequestResult(service.findByUserName(securityUser.getUsername()));
    }
    //修改收藏名称
    @RequestMapping("/collectupdate")
    public RequestResult update(Long id,String collectname){
        MyCollect myCollect = service.findById(id);
        myCollect.setCollectName(collectname);
        service.update(myCollect);
        return new RequestResult();
    }
    //添加收藏
    @RequestMapping("/collectsave")
    public RequestResult save(String collectname,String url) throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser securityUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyCollect myCollect = new MyCollect();
        myCollect.setUserName(securityUser.getUsername());
        myCollect.setCollectName(collectname);
        myCollect.setDate(new Date());
        myCollect.setUrl(url);
        service.update(myCollect);
        return new RequestResult();
    }
    //删除
    @RequestMapping("/deletecollect")
    public RequestResult delete(Long id){
        service.delete(id);
        return new RequestResult();
    }
}
